//import { getUser, removeUser } from "../../dao/auth"

const state = {
    user: null,
    userid: ''
}

const getters = {
    user: state => state.user,
    userid: state => state.userid,
}

const mutations = {
    SET_USER(state, val) {
        state.user = val
    },
    SET_USERID(state, val) {
        state.userid = val
    }
}

const actions = {
    loadUser({ commit }) {
        return new Promise((resolve, reject) => {
            getUser().then(res => {
                if (res) {
                    commit('SET_USER', res)
                    resolve('ok')
                } else {
                    reject()
                }
            }).catch(e => {
                reject(e)
            })
        })
    },
    logout({ commit }) {
        return new Promise((resolve, reject) => {
            removeUser().then(() => {
                commit('SET_USER', null)
                resolve()
            }).catch(e => {
                reject(e)
            })
        })
    }
}

export default {
    state,
    getters,
    mutations,
    actions
}
